﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ProgNetComponents.Design.Attributes;
using ProgNetComponents;
using System.Drawing.Design;
using ProgNetComponents.Interfaces;
using ProgNetComponents.Classes;
using ProgNetComponentsDx.Enums;
using ProgNetComponents.Design.TypeConverters;
using ProgNetComponents.Design.Editors;
using ProgNetComponentsDx.Browsers.Design;
using System.Reflection;

namespace ProgNetComponentsDx.Browsers
{
   
    [TypeConverter(typeof(BrowserColumnTypeConverter))]
    public class BrowserColumn : BaseCustomObject, ICloneable, INamedObject
    {
        private BrowserColumnType _ColumnType;

        #region CTOR
        
        public BrowserColumn()
        {
            this.BrowserId = null;
            this.Caption = null;
            this.ColumnType = BrowserColumnType.TextEdit;
            this.DefaultValue = null;
            this.Expression = null;
            this.FilterType = Enums.FilterType.BeginsWith;
            this.IsSearchField = true;
            this.Name = null;
            this.ValueChecked = null;
            this.ValueUnchecked = null;
            this.Visible = true;
            this.Width = 100;
        }
        
        public BrowserColumn(string name, FilterType filter)
            : this()
        {
            FilterType = filter;
            Name = name;
        }

        public BrowserColumn(string name, FilterType filter, bool visible)
            : this()
        {
            FilterType = filter;
            Name = name;
            Visible = visible;
        }
        public BrowserColumn(string name, FilterType filter, BrowserColumnType type, bool visible)
            : this()
        {
            FilterType = filter;
            Name = name;
            Visible = visible;
            this.ColumnType = type;
        }
        public BrowserColumn(string name, FilterType filter, BrowserColumnType type)
            : this()
        {
            FilterType = filter;
            Name = name;
            this.ColumnType = type;
            if (type == BrowserColumnType.CheckEdit)
            {
                ValueChecked = true;
                ValueUnchecked = false;
            }
        }

        public BrowserColumn(string Name)
            : this()
        {
            this.Name = Name;
        }

        public BrowserColumn(string Name, bool visible, bool isSearchField)
            : this()
        {
            this.Name = Name;
            this.Visible = visible;
            this.IsSearchField = isSearchField;
        }

        public BrowserColumn(string Name, string Caption, string Expression)
            : this()
        {
            this.Name = Name;
            this.Caption = Caption;
            this.Expression = Expression;
        }
       
        public BrowserColumn(string Name, string Caption, string Expression, int width)
            : this()
        {
            this.Name = Name;
            this.Caption = Caption;
            this.Expression = Expression;
            this.Width = width;
        }

        public BrowserColumn(string Name, BrowserColumnType type, object DefaultValue,
            object ValueChecked,
            object ValueUnchecked,
            object BrowserId)
            : this()
        {
            this.Name = Name;
            this.ColumnType = type;
            this.DefaultValue = DefaultValue;
            this.ValueChecked = ValueChecked;
            this.ValueUnchecked = ValueUnchecked;
            this.BrowserId = BrowserId;
        }

        public BrowserColumn(string Name, 
            string Caption, 
            string Expression, 
            FilterType FilterType,
            bool IsSearchField, 
            bool Visible, 
            int Width, 
            BrowserColumnType ColumnType, 
            object DefaultValue, 
            object ValueChecked, 
            object ValueUnchecked, 
            object BrowserId)
        {
            this.Name = Name;
            this.Caption = Caption;
            this.Expression = Expression;
            this.FilterType = FilterType;
            this.IsSearchField = IsSearchField;
            this.Visible = Visible;
            this.Width = Width;
            this.ColumnType = ColumnType;
            this.DefaultValue = DefaultValue;
            this.ValueChecked = ValueChecked;
            this.ValueUnchecked = ValueUnchecked;
            this.BrowserId = BrowserId;
        }

        public BrowserColumn(string Name,
            string Caption,
            string Expression,
            FilterType FilterType,
            bool IsSearchField,
            bool Visible,
            int Width,
            BrowserColumnType ColumnType,
            object DefaultValue)
        {
            this.Name = Name;
            this.Caption = Caption;
            this.Expression = Expression;
            this.FilterType = FilterType;
            this.IsSearchField = IsSearchField;
            this.Visible = Visible;
            this.Width = Width;
            this.ColumnType = ColumnType;
            this.DefaultValue = DefaultValue;
            if (ColumnType == BrowserColumnType.CheckEdit)
            {
                this.ValueChecked = true;
                this.ValueUnchecked = false;
            }
            else
            {
                this.ValueChecked = null;
                this.ValueUnchecked = null;
            }
            this.BrowserId = null;

        }

        public BrowserColumn(string Name,
            string Caption,
            string Expression,
            FilterType FilterType,
            bool IsSearchField,
            bool Visible,
            int Width,
            BrowserColumnType ColumnType)
        {
            this.Name = Name;
            this.Caption = Caption;
            this.Expression = Expression;
            this.FilterType = FilterType;
            this.IsSearchField = IsSearchField;
            this.Visible = Visible;
            this.Width = Width;
            this.ColumnType = ColumnType;
            this.DefaultValue = null;
            if (ColumnType == BrowserColumnType.CheckEdit)
            {
                this.ValueChecked = true;
                this.ValueUnchecked = false;
            }
            else
            {
                this.ValueChecked = null;
                this.ValueUnchecked = null;
            }
            
            this.BrowserId = null;
        }

       
        #endregion

        #region browsable Methods
        private bool IsVisibleOrSearchField()
        {
            return Visible || IsSearchField;
        }
        protected bool IsCheckField()
        {
            return IsVisibleOrSearchField() && ColumnType == BrowserColumnType.CheckEdit;
        }
        #endregion
        [Category(ProgNetComponents.Constants.Categories.ProgNetData)]
        public string Name { get; set; }

        [DefaultValue(null),Category(ProgNetComponents.Constants.Categories.ProgNetDisplay),
        BrowsableByMethod("IsVisibleOrSearchField")]
        public string Caption { get; set; }
        
        [Category(ProgNetComponents.Constants.Categories.ProgNetData)]
        public string Expression
        {
            get;
            set;
        }

        [DefaultValue(BrowserColumnType.TextEdit),Category(ProgNetComponents.Constants.Categories.ProgNetDisplay),
        BrowsableByMethod("IsVisibleOrSearchField")]
        public BrowserColumnType ColumnType
        {
            get { return _ColumnType; }
            set
            {
                _ColumnType = value;
                if (value == BrowserColumnType.CheckEdit)
                {
                    if (ValueChecked == null && BrowserContainer.ServiceProvider != null)
                        ValueChecked = true;
                    if (ValueUnchecked == null && BrowserContainer.ServiceProvider != null)
                        ValueUnchecked = false;
                }
                else
                {
                    ValueChecked = null;
                    ValueUnchecked = null;
                }
            }
        }

        [DefaultValue(true), Category(ProgNetComponents.Constants.Categories.ProgNetData)]
        public bool IsSearchField
        {
            get;
            set;
        }

        [Category("Data"),DefaultValue(FilterType.BeginsWith),
        BrowsableByPropertyValue("IsSearchField", true)]
        public FilterType FilterType
        {
            get;
            set;
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetData),
        BrowsableByPropertyValue("IsSearchField", true), 
        UIEditor(typeof(ObjectEditor)),TypeConverter(typeof(ObjectConverter)),DefaultValue(null)]
        public object DefaultValue
        {
            get;
            set;
        }

        [DefaultValue(true),Category(ProgNetComponents.Constants.Categories.ProgNetDisplay)]
        public bool Visible
        {
            get;
            set;
        }

        [DefaultValue(100), Category(ProgNetComponents.Constants.Categories.ProgNetDisplay), 
        BrowsableByPropertyValue("Visible", true)]
        public int Width { get; set; }

        [Category(ProgNetComponents.Constants.Categories.ProgNetData), 
        BrowsableByMethod("IsCheckField"),
        TypeConverter(typeof(ObjectConverter)),DefaultValue(true)]
        public object ValueChecked
        {
            get;
            set;
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetData),
        BrowsableByMethod("IsCheckField"),
        TypeConverter(typeof(ObjectConverter)),DefaultValue(false)]
        public object ValueUnchecked
        {
            get;
            set;
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetData), 
        BrowsableByPropertyValue("ColumnType", BrowserColumnType.LookUpEdit),
        DefaultValue(null), Editor(typeof(ProgNetComponentsDx.Browsers.Design.Editors.BrowserNameEditor), typeof(UITypeEditor))]
        public object BrowserId
        {
            get;
            set;
        }
       

        #region ICloneable Members

        public object Clone()
        {
            BrowserColumn c = new BrowserColumn();

            c.FilterType = this.FilterType;
            c.IsSearchField = this.IsSearchField;
            c.Visible = Visible;
            c.Name = Name;
            c.Caption = Caption;
            c.Width = Width;
            c.ColumnType = ColumnType;
            c.DefaultValue = DefaultValue;
            c.ValueChecked = ValueChecked;
            c.ValueUnchecked = ValueUnchecked;
            c.BrowserId = BrowserId;
            c.Expression = Expression;

            return c;
        }

        #endregion
    }

    public enum DefaultBrowserColumnSetup {
        CheckEdit
    }
    public class BrowserColumnCollection : ProgNetComponents.Collections.BaseNamedCollection<BrowserColumn>
    {

    }
}
